//
//  USBMusicViewController.m
//  wAudioShare
//
//  Created by 赵帅 on 14-1-9.
//  Copyright (c) 2014年 赵帅. All rights reserved.
//

#import "USBMusicViewController.h"
#import "USBMusicObject.h"
#import "MMProgressHUD.h"

@interface USBMusicViewController ()
<serviceGetUSBMusicListDelegate,servicePlayUSBMusicDelegate>
{
    NSMutableArray * musicArray;
    
    NSTimer * timer;
}

@property (assign) IBOutlet UITableView * tableView;

@end

@implementation USBMusicViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [[GlobalService sharedInstance] getUSBMusicList:self];
    
    timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerMethod) userInfo:nil repeats:YES];
    
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navigation methods
-(NSString *)navigationBarTitle
{
    return NSLocalizedString(@"USB_MUSIC",@"");;
}

-(BOOL)isNewVersionButtonEnabled
{
    return YES;
}

-(IBAction)backButtonPressed:(id)sender
{
    for(USBMusicObject * obj in musicArray)
    {
        [[GlobalService sharedInstance] removeHandler:obj];
    }
    
    [timer invalidate];
    timer = nil;
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark UITableView Delegate & Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [musicArray count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 45;
}

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    else
    {
        for(UIView * subview in [cell.contentView subviews])
        {
            [subview removeFromSuperview];
        }
    }
    
    // Configure the cell.
    
    USBMusicObject * musicOb = (USBMusicObject *)[musicArray objectAtIndex:indexPath.row];
    cell.textLabel.text = [musicOb getMusicTitle];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:[tableView indexPathForSelectedRow] animated:YES];
    [[GlobalService sharedInstance] playUSBMusic:self fileIndex:indexPath.row];
    
    [MMProgressHUD showWithStatus:@"请求发送中"];
}

#pragma mark - service callbacks
-(void)serviceGetUSBMusicListCallBack:(NSDictionary*)dicCallBack
{
    NSDictionary * dic = dicCallBack;
    
    if(dic == nil || [[dic objectForKey:@"responsestatus"] intValue] == [SERVICE_FAIL intValue])
    {
        
        return;
    }
    else if([[dic objectForKey:@"responsestatus"] intValue] == [SERVICE_CONNECTIONFAIL intValue] || [[dic objectForKey:@"responsestatus"] intValue] == [SERVICE_TIMEOUT intValue])
    {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"PopTabBarController" object:self];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ErrorComeBack" object:nil];
    }
    
    musicArray = [[NSMutableArray alloc] init];
    for(int i=0; i<[[dicCallBack objectForKey:@"fileList"] count]; i++)
    {
        NSDictionary * dic = [[dicCallBack objectForKey:@"fileList"] objectAtIndex:i];
       
        USBMusicObject * musicOb = [[USBMusicObject alloc] initWithIndex:i];
        musicOb.infoDic = [NSMutableDictionary dictionaryWithDictionary:dic];
        [musicArray addObject:musicOb];
//        [musicOb updateInfo];
    }
    
    [self.tableView reloadData];
}

-(void)servicePlayUSBMusicCallBack:(NSDictionary*)dicCallBack
{
    if([[dicCallBack objectForKey:@"responsestatus"] intValue] == [SERVICE_SUCCESS intValue])
    {
        [MMProgressHUD dismiss];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"JumptoControlView" object:nil];
    }
    else
    {
        [MMProgressHUD dismissWithError:NSLocalizedString(@"MUSIC_PLAY_FAILED",@"")];
    }
}

#pragma mark - private methods
-(void)timerMethod
{
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
}

@end
